/**
 * 
 */
package com.expense.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Administrator
 * 日期时间处理类
 */
public class TimeUtil {
	/**Date转换字符串**/
	public static String dateToString(Date date,String sdfString) {
		String string="";
		SimpleDateFormat sdf=new SimpleDateFormat(sdfString);
		try {
			string=sdf.format(date);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return string;
	}
	
	/**字符串转换Date**/
	public static Date stringToDate(String string,String sdfString) {
		Date date=null;
		SimpleDateFormat sdf=new SimpleDateFormat(sdfString);
		try {
			date=sdf.parse(string);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}
	
	/**得到某月最大天**/
	public static Integer maxDayByMonth(Integer year,Integer month) {
		Integer maxDay=null;
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date=sdf.parse(year+"-"+month+"-"+1);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			maxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return maxDay;
	}
	
	/**12小时制上午下午处理**/
	public static String timeConvert(String dateTime) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String tm=dateTime.replace("上午", "").replace("下午", "");
			if(dateTime.indexOf("/")!=-1) {
				if(tm.length()<11) {
					SimpleDateFormat sdfH=new SimpleDateFormat("MM/dd/yyyy");
					Date dateH=sdfH.parse(tm);
					tm=sdf.format(dateH);
				}else {
					SimpleDateFormat sdfH=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
					Date dateH=sdfH.parse(tm);
					tm=sdf.format(dateH);
				}
			}
			
			Date date=sdf.parse(tm);
			if (dateTime.indexOf("下午")!=-1&&date.getHours()!=12) {
				date.setHours(date.getHours()+12);
			} 
			
			dateTime=sdf.format(date);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return dateTime;
	}
	
	/**根据日期得到星期几**/
	public static int getWeekInt(Date date) {
        if (date == null)
            return -1;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        return cetemp.get(Calendar.DAY_OF_WEEK) - 1;
    }
	
	/**根据日期得到月份**/
	public static int getMonthInt(Date date) {
        if (date == null)
            return -1;
        Calendar cetemp = Calendar.getInstance();
        cetemp.setTime(date);
        return cetemp.get(Calendar.MONTH) + 1;
    }
	
	/**根据日期得到日**/
	 public static int getDayInt(Date date) {
	        if (date == null)
	            return -1;
	        Calendar cetemp = Calendar.getInstance();
	        cetemp.setTime(date);
	        return cetemp.get(Calendar.DAY_OF_MONTH);
	 }
	 
	 
	 /**根据日期得到年**/
	 public static int getYearInt(Date date) {
	        if (date == null)
	            return -1;
	        Calendar cetemp = Calendar.getInstance();
	        cetemp.setTime(date);
	        return cetemp.get(Calendar.YEAR);
	 }
	 
	 /**当前日期周的第一天**/
	 public static Date getFirstWeekDate(Date date) {
	        if (date == null)
	            return null;
	        Calendar cetemp = Calendar.getInstance();
	        cetemp.setTime(date);
	        cetemp.add(Calendar.DATE, 1 - cetemp.get(Calendar.DAY_OF_WEEK));
	        return cetemp.getTime();
	  }
	 /**当前日期周的最后一天**/
	 public static Date getLastWeekDate(Date date) {
	        if (date == null)
	            return null;
	        Calendar cetemp = Calendar.getInstance();
	        cetemp.setTime(date);
	        cetemp.add(Calendar.DATE, 7 - cetemp.get(Calendar.DAY_OF_WEEK));
	        return cetemp.getTime();
	   }
	 
	 /**当前日期所在月的第一天**/
	 public static Date getFirstMonthDate(Date date) {
	        if (date == null)
	            return null;
	        Calendar cetemp = Calendar.getInstance();
	        cetemp.setTime(date);
	        cetemp.add(Calendar.DATE, 1 - cetemp.get(Calendar.DAY_OF_MONTH));
	        return cetemp.getTime();
	 }
	 
	 /**当前日期所在月的最后一天**/
	 public static Date getLastMonthDate(Date date) {
	        if (date == null)
	            return null;
	        Calendar cetemp = Calendar.getInstance();
	        cetemp.setTime(date);
	        cetemp.set(Calendar.MONTH, cetemp.get(Calendar.MONTH) + 1);
	        cetemp.add(Calendar.DATE, -cetemp.get(Calendar.DAY_OF_MONTH));
	        return cetemp.getTime();
	  }
	 /**当前日期所在季度的第一天**/
	 public static Date getFirstQuarterDate(Date date) {
	        if (date == null)
	            return null;
	        Calendar cetemp = Calendar.getInstance();
	        cetemp.setTime(date);
	        cetemp.set(Calendar.MONTH, cetemp.get(Calendar.MONTH) - (cetemp.get(Calendar.MONTH)) % 3);
	        cetemp.add(Calendar.DATE, 1 - cetemp.get(Calendar.DAY_OF_MONTH));
	        return cetemp.getTime();
	  }
	 /**当前日期所在季度的最后一天**/
	 public static Date getLastQuarterDate(Date date) {
	        if (date == null)
	            return null;
	        Calendar cetemp = Calendar.getInstance();
	        cetemp.setTime(date);
	        cetemp.set(Calendar.MONTH, cetemp.get(Calendar.MONTH) - (cetemp.get(Calendar.MONTH)) % 3 + 3);
	        cetemp.add(Calendar.DATE, -cetemp.get(Calendar.DAY_OF_MONTH));
	        return cetemp.getTime();
	   }
	 /**当前日期所在年的第一天**/
	 public static Date getFirstYearDate(Date date) {
	        if (date == null)
	            return null;
	        Calendar cetemp = Calendar.getInstance();
			cetemp.setTime(date);
			cetemp.set(Calendar.MONTH,0);
			cetemp.add(Calendar.DATE,1 - cetemp.get(Calendar.DAY_OF_MONTH));
			return cetemp.getTime();
		}
	 
	 /**当前日期所在年的最后一天**/
	 public static Date getLastYearDate(Date date) {
			if (date == null)
				return null;
			Calendar cetemp = Calendar.getInstance();
			cetemp.setTime(date);
			cetemp.set(Calendar.MONTH,12);
			cetemp.add(Calendar.DATE, - cetemp.get(Calendar.DAY_OF_MONTH));
			return cetemp.getTime();
		}
	
	 /**当前日期的后n天**/
	 public static Date getSecondDate(Date date, int n) {
	        if (date == null)
	            return null;
	        Calendar cetemp = Calendar.getInstance();
	        cetemp.setTime(date);
	        cetemp.add(Calendar.DATE, n);
	        return cetemp.getTime();
	    }
	 
	 /**计算两个日期之间相差几天**/
	 public static int compareToday(Date startDate, Date endDate) {
	        int time = 24 * 60 * 60 * 1000;
	        int diff = (int) ((endDate.getTime() - startDate.getTime()) / time);
	        return diff;
	    }
	 
	 /** 计算某一天是一年中的第几星期  **/
	 public static int getWeekNo(Date date){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(date);
		 return cal.get(Calendar.WEEK_OF_YEAR);
	 }

	 /**改变年**/
	 public static Date  setDateChangYear(Date date,int changYear) {
		 Calendar cetemp = Calendar.getInstance();
		 cetemp.setTime(date);
		 cetemp.set(Calendar.YEAR,cetemp.get(Calendar.YEAR)+changYear);	
		return cetemp.getTime();	
	 }
	 /**改变年**/
	 public static Date  setDateChangMonth(Date date,int changMonth) {
		 Calendar cetemp = Calendar.getInstance();
		 cetemp.setTime(date);
		 cetemp.set(Calendar.MONTH,cetemp.get(Calendar.MONTH)+changMonth);	
		return cetemp.getTime();	
	 }
	 
	 /**改变日**/
	 public static Date  setDateChangDay(Date date,int changDay) {
		 Calendar cetemp = Calendar.getInstance();
		 cetemp.setTime(date);
		 cetemp.set(Calendar.DATE,cetemp.get(Calendar.DATE)+changDay);	
		return cetemp.getTime();	
	 }
	 
	public static void main(String[] args) {
		//Date date=new Date();
		//System.out.println(setDateChangDay(date,1).toLocaleString());
	System.out.println(TimeUtil.getYearInt(new Date()));
	}
}
